<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Video to MP3 Converter</title>
    <!-- 引入ffmpeg.min.js -->
    <script src="https://cdn.jsdelivr.net/npm/@ffmpeg/ffmpeg@0.10.1/dist/ffmpeg.min.js"></script>
    <style>
        /* 进度条样式 */
        #progressContainer {
            width: 100%;
            background-color: #ddd;
        }

        #progressBar {
            width: 1%;
            height: 30px;
            background-color: #4CAF50;
            text-align: center;
            color: white;
        }
    </style>
</head>
<body>
    <input type="file" id="fileUploader" accept="video/*" />
    <button id="convertBtn">Convert to MP3</button>
    <div id="progressContainer" style="display:none;">
        <div id="progressBar">0%</div>
    </div>
    <button id="downloadBtn" style="display:none;">Download MP3</button>
    <audio id="audioPlayer" controls style="display:none;"></audio> <!-- 添加音频播放器 -->

<script>
    const { createFFmpeg, fetchFile } = FFmpeg;
    const ffmpeg = createFFmpeg({ 
        log: true,
        progress: ({ ratio }) => {
            // 更新进度条
            const progress = Math.floor(ratio * 100);
            const progressBar = document.getElementById('progressBar');
            progressBar.style.width = progress + '%';
            progressBar.textContent = progress + '%';
        } 
    });

    document.getElementById('convertBtn').addEventListener('click', async () => {
        const fileUploader = document.getElementById('fileUploader');
        const files = fileUploader.files;
        if (files.length === 0) {
            alert("Please select a file.");
            return;
        }
        const file = files[0];

        // 显示进度条容器
        document.getElementById('progressContainer').style.display = 'block';

        const tempFileName = `temp_${Date.now()}`;
        const outputFileName = `${tempFileName}.mp3`;

        if (!ffmpeg.isLoaded()) await ffmpeg.load();

        ffmpeg.FS('writeFile', tempFileName, await fetchFile(file));

        await ffmpeg.run('-i', tempFileName, '-vn', '-ar', '44100', '-ac', '2', '-b:a', '192k', outputFileName);

        const data = ffmpeg.FS('readFile', outputFileName);
        const url = URL.createObjectURL(new Blob([data.buffer], { type: 'audio/mp3' }));

        // 设置下载按钮
        const downloadBtn = document.getElementById('downloadBtn');
        downloadBtn.onclick = function() { // 使用onclick事件监听
            const a = document.createElement('a');
            a.href = url;
            a.download = `${file.name.split('.')[0]}.mp3`;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        };
        downloadBtn.style.display = 'block'; // 显示下载按钮

        // 设置音频播放器
        const audioPlayer = document.getElementById('audioPlayer');
        audioPlayer.src = url;
        audioPlayer.style.display = 'block'; // 显示音频播放器

        // Cleanup
        document.getElementById('progressContainer').style.display = 'none'; // 转换完成后隐藏进度条
    });
</script>
</body>
</html>
